//引入axios实例
import axios from 'axios'
import qs from 'qs'

axios.defaults.baseURL = process.env.VUE_APP_BASEURL;
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';

/** 响应结果 */
axios.interceptors.response.use(
   response => {
      if (response.status === 200) {
         return response.data;
      } else {
         return Promise.reject(response);
      }
   },
   error => {
      return Promise.reject(error);
   }
);

const isData = ['POST', 'PUT', 'PATCH'];
const http = {
   _header(method = "GET", url, data, config) {
      data = data || {};
      config = config || {};
      method = method.toLocaleUpperCase();
      if (isData.indexOf(method) >= 0) {
         config.data = data;
      } else {
         config.params = data;
      }
      return axios.request({
         ...config,
         url,
         method,
      });
   },
   get(url = "", data = {}, config) {
      return http._header("get", url, data, config);
   },
   post(url = "", data = {}, config) {
      return http._header("post", url, data, config);
   },
   delete(url = "", data = {}, config) {
      return http._header("delete", url, data, config);
   },
   put(url = "", data = {}, config) {
      return http._header("put", url, data, config);
   },
   patch(url = "", data = {}, config) {
      return http._header("patch", url, data, config);
   },
};
export default http;

